﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PCLawPrototype
{
    public partial class NewLawyerFrm : Form
    {
        private bool _isUpdated = false;
        private Lawyer _editLawyer;
        private List<LawyerRank> _lawyerRank = new List<LawyerRank>();

        public NewLawyerFrm()
        {
            InitializeComponent();
            _editLawyer = new Lawyer();


            for (int i = char.ConvertToUtf32("A", 0); i <= char.ConvertToUtf32("F", 0); i++)
            {
                _lawyerRank.Add(new LawyerRank()
                                    {
                                        //Lawyer = _editLawyer,
                                        RankValue = ((char)(i)).ToString(),
                                        RateValue = 0.0,
                                        Money = 0.0
                                    });
            }

            FillRankInformation(_lawyerRank);
        }

        public NewLawyerFrm(Lawyer lawyer)
        {
            InitializeComponent();

            _isUpdated = true;
            _editLawyer = lawyer;
            this.Text = "Edit Lawyer [" + lawyer.LawyerCode + " - " + lawyer.LawyerName + "]";
            txtLCode.Text = lawyer.LawyerCode;
            txtLName.Text = lawyer.LawyerName;

            _editLawyer.LawyerRanks.Load();
            _lawyerRank = _editLawyer.LawyerRanks.ToList();

            FillRankInformation(_lawyerRank);
        }

        private void FillRankInformation(List<LawyerRank> lawyerRank)
        {
            lstRank.Items.Clear();

            foreach (LawyerRank rank in lawyerRank)
            {
                lstRank.Items.Add(new ListViewItem(new string[]
                                                       {
                                                           rank.RankValue,
                                                           rank.RateValue.ToString(),
                                                           rank.Money.ToString()
                                                       },-1));
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Data has been saved!");
            if (!_isUpdated)
            {
                Lawyer lawyer = new Lawyer()
                                    {
                                        LawyerID = Guid.NewGuid().ToString(),
                                        LawyerDailyGoal = int.Parse(cbxDailyGoal.Text),
                                        LawyerInitial = txtInitial.Text,
                                        LawyerName = txtLName.Text,
                                        LawyerCode = txtLCode.Text,
                                        CreatedDate = DateTime.Now,
                                        UpdatedDate = DateTime.Now
                                    };

                foreach (LawyerRank lawyerRank in _lawyerRank)
                {
                    lawyerRank.Lawyer = lawyer;
                    lawyerRank.CreatedDate = DateTime.Now;
                    lawyerRank.UpdatedDate = DateTime.Now;

                    lawyer.LawyerRanks.Add(lawyerRank);
                }

                Common.Entity.AddToLawyers(lawyer);
            }
            else
            {
                _editLawyer.LawyerDailyGoal = int.Parse(cbxDailyGoal.Text);
                _editLawyer.LawyerInitial = txtInitial.Text;
                _editLawyer.LawyerName = txtLName.Text;
                _editLawyer.LawyerCode = txtLCode.Text;
                _editLawyer.UpdatedDate = DateTime.Now;

            }

            Common.Entity.SaveChanges();
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnRateHistory_Click(object sender, EventArgs e)
        {
            EditRankInformation editRankInformation = new EditRankInformation(_lawyerRank);
            editRankInformation.ShowDialog();

            FillRankInformation(_lawyerRank);
        }

        private void btnHelp_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Opening Help File!");
        }
    }
}
